<template>
	<a-modal :visible="visible" width="500px" title="报废" centered destroyOnClose @cancel="cancel" :footer="null" :maskClosable="false" wrapClassName="a-modal-dialog">
		<a-form ref="formRef" :model="modelRef" :rules="formRules" class="flex-container-nomal" style="max-height: 70vh" @finish="run" :labelCol="{style: {width: '90px'}}">
			<div class="flex1 auto-y">
				<a-row>
					<a-col :span="12">
						<a-form-item label="物品名称">
							{{ detailInfo.configName }}
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="数量">
							{{ detailInfo.count }}
						</a-form-item>
					</a-col>
					<a-col :span="24">
						<a-form-item label="所在位置">
							{{ detailInfo.currentPosition }}
						</a-form-item>
					</a-col>
					<a-col :span="24">
						<a-form-item label="库房名称" name="warehouseId" :rules="[{required: true, message: '请选择库房'}]">
							<a-select :options="wareHouseList" v-model:value="modelRef.warehouseId" placeholder="请选择库房" :field-names="{label: 'name', value: 'id'}" />
						</a-form-item>
					</a-col>
				</a-row>
			</div>
			<div class="a-modal-dialog-footer">
				<a-space :size="12">
					<a-space :size="12">
						<a-button @click="cancel">取消</a-button>
						<a-button type="primary" html-type="submit" :loading="loading" :disabled="loading">确定</a-button>
					</a-space>
				</a-space>
			</div>
		</a-form>
	</a-modal>
</template>
<script setup lang="ts">
	import {reactive, ref, watch} from 'vue'
	import {useRequest} from 'vue-request'
	import {houseConfigDetail, houseConfigAbolish, getWareHouseList} from '@/api/dictionary'
	import {message} from 'ant-design-vue'

	const props = defineProps({
		visible: {
			type: Boolean,
			required: true
		},
		id: {
			type: [String, Number],
			default: ''
		}
	})
	const emit = defineEmits(['update:visible', 'success'])
	const cancel = () => {
		emit('update:visible', false)
		resetForm()
	}
	watch(
		() => props.visible,
		(newVal: boolean) => {
			if (newVal) {
				getDetail()
			}
		}
	)

	const modelRef = reactive({
		abolishMode: 1,
		warehouseId: null
	})

	const formRules = {}

	const wareHouseList = ref<any>([])

	const detailInfo = ref<any>({})
	const getDetail = () => {
		// 获取库房列表
		getWareHouseList({}).then((res: any) => {
			wareHouseList.value = res.data
			console.log(res, '库房列表')
		})
		houseConfigDetail(props.id).then((res: any) => {
			detailInfo.value = res.data
		})
	}

	const sendRequest = () => {
		let params: any = {...modelRef, id: props.id}
		console.log(modelRef, modelRef.warehouseId)
		return houseConfigAbolish(params)
	}

	const {run, loading} = useRequest(sendRequest, {
		manual: true,
		formatResult: (res: any) => {
			if (res.code == 200) {
				message.success('操作成功')
				setTimeout(() => {
					emit('success')
					cancel()
				}, 300)
			}
		}
	})

	// 重置表单
	const formRef = ref<any>()
	const resetForm = () => {
		formRef.value.resetFields()
		detailInfo.value = {}
	}
</script>
